package com.example.java_chatroom.api;

import com.example.java_chatroom.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
public class FriendRequestAPI {
    @Autowired
    private FriendReqMapper friendReqMapper;

    @Autowired
    private FriendMapper friendMapper;

//    @GetMapping("/getAddFriendList")
//    public List<FriendReq> getAddFriendList(@SessionAttribute("user") User user) {
//        List<FriendReq> resp = friendReqMapper.getInitiator(user.getUserId());
//        for (FriendReq e : resp) {
//            e.setState(0);
//        }
//
//        List<FriendReq> resp2 = friendReqMapper.getReceiver(user.getUserId());
//        for (FriendReq e : resp2) {
//            e.setState(1);
//        }
//
//        resp.addAll(resp2);
//        return resp;
//    }

    @GetMapping("/searchUser")
    public List<FriendReq> searchUserList(String searchElement, @SessionAttribute("user") User user) {
        System.out.println("[searchElement] username = " + searchElement);
        // 1. 获取到 resp
        List<FriendReq> resp = friendReqMapper.getSearchList(searchElement, user.getUsername());
        Set<Integer> friendSet = new HashSet<>();
        Set<Integer> initiatorSet = new HashSet<>();
        Set<Integer> receiverSet = new HashSet<>();

        List<Friend> friends = friendMapper.selectFriendList(user.getUserId());
        for (Friend friend : friends) {
            friendSet.add(friend.getFriendId());
        }

        List<FriendReq> initiators = friendReqMapper.getInitiator(user.getUserId());
        for (FriendReq initiator : initiators) {
            initiatorSet.add(initiator.getUserId());
        }

        List<FriendReq> receivers = friendReqMapper.getReceiver(user.getUserId());
        for (FriendReq receiver : receivers) {
            receiverSet.add(receiver.getUserId());
        }

        for (FriendReq friendReq : resp) {
            if (friendSet.contains(friendReq.getUserId())) {
                friendReq.setState(3);
            } else if (initiatorSet.contains(friendReq.getUserId())) {
                friendReq.setState(0);
            } else if (receiverSet.contains(friendReq.getUserId())) {
                friendReq.setState(1);
            } else {
                friendReq.setState(2);
            }
        }

        return resp;
    }

    @PostMapping("/sendFriendReq")
    public synchronized void sendFriendReq(String message, String receiverName, @SessionAttribute("user") User user) {
        // 查询是否重复添加
        List<Integer> reqList = friendReqMapper.searchFriend(user.getUserId());
        // 查找 personName 对应的 userId
        int receiverId = friendReqMapper.getUserIdByName(receiverName);
        for (int x : reqList) {
            if (x == receiverId) {
                // 如果该用户已经向对方发起过请求了, 就 return
                return;
            }
        }
        // 构造插入数据库数据
        FriendReq friendReq = new FriendReq();
        friendReq.setInitiator(user.getUserId());
        friendReq.setReceiver(receiverId);
        friendReq.setMessage(message);
        // 插入到关系表中
        friendReqMapper.addFriendReq(friendReq);
        System.out.println("[sendFriendReq] 已将申请关系插入表中: " + friendReq);
    }

    @GetMapping("/getFriendReq")
    public List<FriendReq> getFriendReq(@SessionAttribute("user") User user) {
        // 只查找 receiver 为当前用户的 initiator
        return friendReqMapper.getInitiator(user.getUserId());
    }

    @PostMapping("/rejectFriendReq")
    public void rejectReq(@RequestParam("requestId") Integer initiator, @SessionAttribute("user") User user) {
        // 删除数据库中对应的数据
        friendReqMapper.deleteData(initiator, user.getUserId());
    }

    @PostMapping("/acceptFriendReq")
    @Transactional
    public void acceptReq(@RequestParam("requestId") Integer friendId, @SessionAttribute("user") User user) {
        // 添加好友
        friendMapper.addFriend(user.getUserId(), friendId);
        // 删除申请表的记录
        friendReqMapper.deleteData(friendId, user.getUserId());
    }

}
